<template>
	<view class="diy-popup-content">
		<view class="title-row">
			<text style="color: #a3aab4;" @click="close(false)">关闭</text>
			<text style="font-weight: bold;">DIY</text>
			<text style="color: #3571fb;" @click="confirmDiy">确认</text>
		</view>
		<view class="msg-row input-row">
			<view class="left">
				<iconfont :name="'type'" :size="25"></iconfont>
				<text>物品</text>
			</view>
			<view class="right">
				<uni-easyinput v-model="diyName" placeholder="请输入物品名称" />
			</view>
		</view>
		<view class="msg-row img-row">
			<view class="left">
				<iconfont :name="'addimg'" :size="25"></iconfont>
				<text>图片</text>
			</view>
			<view class="right">
				<view class="img-box" v-if="imgUrl!=''">
					<uni-icons type="refreshempty" size="30" color="#c4c4c4" @click="resetImg"></uni-icons>
					<image :src="imgUrl" style="width: 80px; height: 80px;" @click="previewImage">
					</image>
				</view>

				<view class="icon-box" v-else>
					<uni-icons type="plusempty" color="#c4c4c4" size="80" @click="uploadPic"></uni-icons>
				</view>
			</view>
		</view>
		<view class="msg-row other-row">
			<view class="row-item">
				<view class="left">
					<uni-icons type="calendar" size="25"></uni-icons>
					<text>保质期</text>
				</view>
				<view class="right">
					<my-number-box :background="'#fff'" :value="warrantTime" :min="1" :max="999" @change="changeDay" />
					<view class="unit-box">
						<text>天</text>
					</view>
				</view>
			</view>
			<view class="row-item">
				<view class="left">
					<iconfont :name="'location'" :size="25"></iconfont>
					<text>位置</text>
				</view>
				<view class="right">
					<uni-data-picker ref="picker" placeholder="请选择放置的位置" popup-title="请选择放置的位置" :localdata="dataTree"
						:modelValue="location" :clearIcon="false" @change="onchange" @popupopened="popupopened"
						@popupclosed="popupclosed">
					</uni-data-picker>
				</view>
			</view>

		</view>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex'

	export default {
		name: "my-diy-pop",
		props: {
			dataTree: {}
		},
		data() {
			return {
				diyName: '',
				imgUrl: '',
				warrantTime: 1,
				// 存放位置
				location: '1-1',
			};
		},
		computed: {
			...mapState('user', ['token']),
			...mapState('fridge', ['default_fridge'])
		},
		methods: {
			// 请求
			uploadPic() {
				uni.chooseImage({
					count: 1,
					sizeType: ['compressed'], //可以指定是原图还是压缩图，默认二者都有
					success: (chooseImageRes) => {
						// 压缩
						uni.compressImage({
							src: chooseImageRes.tempFilePaths[0],
							quality: 40,

							success: (res) => {
								this.imgUrl = res.tempFilePath
								uni.uploadFile({
									url: uni.$uploadPath + "/upload/objectImg",
									filePath: res.tempFilePath,
									name: 'objectImg',
									header: {
										"token": this.token
									},
									formData: {},
									success: (res) => {
										if (res.statusCode !== 200)
											return uni.$showMsg('服务器繁忙')
										const {
											data: result
										} = JSON.parse(res.data)
										this.imgUrl = uni.$staticResourcePath + result
									},
									fail: (res) => {
										uni.$showMsg('服务器域名出错')
										console.log(res.errMsg);
									}
								});
							}
						})
					}
				});
			},
			confirmDiy() {
				const obj = {
					fid: this.default_fridge.fid,
					name: this.diyName,
					imgSrc: this.imgUrl.replace(uni.$staticResourcePath, ""),
					warrantTime: this.warrantTime,
					location: this.location == '1-1' ? this.dataTree[0].text + "-" + this.dataTree[0]
						.children[0].text : this.location[0].text + "-" + this.location[1].text,
					selfDefined: true
				}
				uni.$api.object.diy(obj)
				this.close(true)
			},

			// 事件处理
			close(flag) {
				this.diyName = ''
				this.imgUrl = ''
				this.warrantTime = 1
				this.location = '1-1'
				this.$emit('close', flag)
			},
			previewImage() {
				uni.previewImage({
					urls: [this.imgUrl]
				});
			},
			resetImg() {
				this.imgUrl = ''
			},
			// 保质期
			changeDay(value) {
				this.warrantTime = value
			},
			// 位置
			onchange(e) {
				this.location = e.detail.value
			},
			// 处理双层弹窗的遮罩层响应问题
			popupopened(e) {
				this.$emit('popupopened')
			},
			popupclosed(e) {
				this.$emit('popupclosed')
			},
		}
	}
</script>

<style lang="scss">
	.diy-popup-content {
		border-radius: 20rpx 20rpx 0 0;
		background-color: #e5e6eb;
		padding: 20rpx;
		display: flex;
		flex-direction: column;
		align-items: center;

		.title-row {
			display: flex;
			justify-content: space-between;
			align-items: center;
			font-size: 22px;
			width: 98%;
		}

		.msg-row {
			background-color: #fff;
			border-radius: 12rpx;
			display: flex;
			justify-content: space-between;
			width: 86%;
			padding: 25rpx 6%;
			font-size: 20px;
			align-items: center;
			margin-top: 20rpx;

			.left {
				>text {
					margin-left: 10rpx;
				}
			}
		}

		.img-row {
			.right {
				width: 110px;
				height: 80px;
				align-items: center;
				border-radius: 12rpx;

				.img-box {
					display: flex;
					align-items: center;
				}

				.icon-box {
					margin-left: 30px;
					width: 80px;
					background-color: #e5e6e5;
					border-radius: 12rpx;
				}

			}
		}

		.other-row {
			display: flex;
			flex-direction: column;

			.row-item {
				width: 100%;
				display: flex;
				justify-content: space-between;
				margin: 20rpx;
				align-items: center;

				.right {
					display: flex;
					align-items: center;
				}
			}
		}
	}
</style>